package com.erps.CommonUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.erps.sys.rest.PageInfo;

public class RequestUtils {

	public static void ReadFromRequest(Object obj, HttpServletRequest request) {
		Method ms[] = obj.getClass().getDeclaredMethods();
		String mname;
		String field;
		String fieldType;
		String value;
		for (Method m : ms) {
			mname = m.getName();
			if (!mname.startsWith("set") || ArrayUtils.isEmpty(m.getParameterTypes())) {
				continue;
			}
			try {
				field = mname.toLowerCase().charAt(3) + mname.substring(4, mname.length());
				value = request.getParameter(field.toLowerCase());
				if (StringUtils.isEmpty(value)) {
					continue;
				}
				fieldType = m.getParameterTypes()[0].getName();
				// 以下可以确认value为String类型
				if (String.class.getName().equals(fieldType)) {
					m.invoke(obj, (String) value);
				} else if (Integer.class.getName().equals(fieldType) && NumberUtils.isDigits((String) value)) {
					m.invoke(obj, Integer.valueOf((String) value));
				} else if (Short.class.getName().equals(fieldType) && NumberUtils.isDigits((String) value)) {
					m.invoke(obj, Short.valueOf((String) value));
				} else if (Float.class.getName().equals(fieldType) && NumberUtils.isNumber((String) value)) {
					m.invoke(obj, Float.valueOf((String) value));
				} else if (Double.class.getName().equals(fieldType) && NumberUtils.isNumber((String) value)) {
					m.invoke(obj, Double.valueOf((String) value));
				} else if (Date.class.getName().equals(fieldType)) {
					m.invoke(obj, DateUtils.parseDate((String) value, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"));
				} else {
					m.invoke(obj, value);
				}
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
	}

	public static JActiveTable getFromRequest(JActiveTable aTable, HttpServletRequest request) {
		ArrayList<JActiveField> FieldList = aTable.getFields();
		int iLength = (FieldList == null) ? 0 : FieldList.size();
		for (int i = iLength - 1; i >= 0; i--) {
			JActiveField vField = FieldList.get(i);
			String cFieldName = vField.getFieldName();
			String cFieldValue = StringEx.getStr(request.getParameter(cFieldName));
			vField.setFieldValue(cFieldValue);
			aTable.getFields().set(i, vField);
		}
		return aTable;
	}

	public static PageInfo getPage(HttpServletRequest request) {
		PageInfo vPage = new PageInfo();
		vPage.setPageno(StringEx.getInt(request.getParameter("__pageno"), 1));
		vPage.setPagesize(StringEx.getInt(request.getParameter("__pagesize"), 15));
		return vPage;
	}

	public static JActiveTable getFromRequest(Object obj, HttpServletRequest request) {
		JActiveTable aTable = new JActiveTable();
		Method ms[] = obj.getClass().getDeclaredMethods();
		String mname;
		String cFieldName;
		String cFieldValue;
		for (Method m : ms) {
			mname = m.getName();
			if (!mname.startsWith("set") || ArrayUtils.isEmpty(m.getParameterTypes())) {
				continue;
			}
			try {
				cFieldName = mname.toLowerCase().charAt(3) + mname.substring(4, mname.length());
				String[] ValueList = request.getParameterValues(cFieldName.toString());
				if (ValueList != null) {
					cFieldValue = "";
					for (int i = 0; i < ValueList.length; i++) {
						if (cFieldValue.length() == 0)
							cFieldValue = ValueList[i];
						else
							cFieldValue = cFieldValue + " " + ValueList[i];
					}
				} else {
					cFieldValue = request.getParameter(cFieldName.toLowerCase());
				}

				if (StringUtils.isEmpty(cFieldValue)) {
					aTable.addFieldValue(cFieldName, "");
					continue;
				} else {
					aTable.addFieldValue(cFieldName, (String) cFieldValue);
				}
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
		return aTable;
	}
}
